package com.solution.admin.customer.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.customer.entity.CompanyExpressConfigAddressEntity;
import com.solution.admin.customer.service.CompanyExpressConfigAddressService;
import com.solution.common.constants.PublicStatus;
import com.solution.common.controller.BaseController;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.string.StringUtil;

/**
 * 公司常用物流配置-地址列表web控制层
 * @author solin
 * @date 2018-03-22 18:51:24
 */
@RequestMapping("/companyExpressConfigAddress")
@Controller
public class CompanyExpressConfigAddressController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(CompanyExpressConfigAddressController.class);

	@Autowired
	private CompanyExpressConfigAddressService companyExpressConfigAddressService;
	
	/**
	 * 跳转公司常用物流配置-地址列表查询页面
	 * @param CompanyExpressConfigAddress 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author solin
 	 * @date 2018-03-22 18:51:24
	 */
	@RequestMapping("/toListCompanyExpressConfigAddressPage")
	public String toListCompanyExpressConfigAddressPage(CompanyExpressConfigAddressEntity companyExpressConfigAddress, HttpServletRequest request) {
		return "customer/companyExpressConfigAddress_list";
	}

	/**
	 * 分页查询公司常用物流配置-地址列表列表
	 * @param companyExpressConfigAddress 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author solin
 	 * @date 2018-03-22 18:51:24
	 */
	@RequestMapping("/listCompanyExpressConfigAddressPage")
	@ResponseBody
	public JSONGrid listCompanyExpressConfigAddressPage(CompanyExpressConfigAddressEntity companyExpressConfigAddress, HttpServletRequest request){
		int pageNum=Integer.parseInt(request.getParameter("page").toString());
		int numPerPage=Integer.parseInt(request.getParameter("rows").toString());
		PageParam pageParam = super.getPageParam(request);
		pageParam.setPageNum(pageNum);
		pageParam.setNumPerPage(numPerPage);
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		try {
			PageBean pageBean = companyExpressConfigAddressService.listCompanyExpressConfigAddressPage(pageParam, paramMap);
			return new JSONGrid(pageBean.getRecordList(),pageBean.getTotalCount());
		} catch (Exception e) {
			handleException("分页查询公司常用物流配置-地址列表失败", logger, e);
		}
		return new JSONGrid();
	}
	
	/**
	 * 获取公司常用物流配置-地址列表列表
	 * @param companyExpressConfigAddress
	 * @param request
	 * @return
	 * @author: solin
	 * @date: 2018年3月23日 下午4:04:44
	 */
	@RequestMapping("/listCompanyExpressConfigAddress")
	@ResponseBody
	public Map<String, Object> listCompanyExpressConfigAddress(CompanyExpressConfigAddressEntity companyExpressConfigAddress, HttpServletRequest request){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		try {
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("companyId", request.getParameter("companyId"));
			paramMap.put("expressType", request.getParameter("expressType"));
			paramMap.put("expressConfigId", request.getParameter("expressConfigId"));
			List<CompanyExpressConfigAddressEntity> companyExpressConfigAddressList = companyExpressConfigAddressService.listCompanyExpressConfigAddress(paramMap);
			resultMap.put("data", companyExpressConfigAddressList);
			resultMap.put(PublicStatus.RESULT_CODE, companyExpressConfigAddressList != null?"1":"0");
			resultMap.put(PublicStatus.RESULT_MSG, companyExpressConfigAddressList != null?"操作成功":"操作失败");
		} catch (Exception e) {
			handleException("查询公司常用物流地址失败", logger, e);
			resultMap.put(PublicStatus.RESULT_CODE, "0");
			resultMap.put(PublicStatus.RESULT_MSG, "操作失败");
		}
		
		return resultMap;
	}
	
	
	/**
	 * 跳转新增页面
	 * @return string 跳转页面路径
	 * @author solin
 	 * @date 2018-03-22 18:51:24
	 */
	@RequestMapping("/toAddCompanyExpressConfigAddress")
	public String toAddCompanyExpressConfigAddress(HttpServletRequest request) {
		request.setAttribute("opFlag", "add");
		return "customer/companyExpressConfigAddress_form";
	}

	/**
	 * 跳转编辑页面
	 * @return string 跳转页面路径
	 * @author solin
 	 * @date 2018-03-22 18:51:24
	 */
	@RequestMapping("/toUpdateCompanyExpressConfigAddress")
	public String toUpdateCompanyExpressConfigAddress(HttpServletRequest request) {
		// 编辑操作
		request.setAttribute("opFlag", "edit");
		long id = Long.valueOf(request.getParameter("id"));
		try {
			CompanyExpressConfigAddressEntity companyExpressConfigAddress = companyExpressConfigAddressService.getCompanyExpressConfigAddressById(id);
			request.setAttribute("companyExpressConfigAddress", companyExpressConfigAddress);
		} catch (Exception e) {
			handleException("跳转编辑公司常用物流配置-地址列表页面错误", logger, e);
		}
		return "customer/companyExpressConfigAddress_form";
	}


	/**
	 * 新增或保存公司常用物流配置-地址列表
	 * @param request 请求对象
	 * @param companyExpressConfigAddressEntity 公司常用物流配置-地址列表实体
	 * @return
	 * @author solin
 	 * @date 2018-03-22 18:51:24
	 */
	@RequestMapping("/saveOrUpdateCompanyExpressConfigAddress")
	@ResponseBody
	public Map<String, Object> saveCompanyExpressConfigAddress(HttpServletRequest request, CompanyExpressConfigAddressEntity companyExpressConfigAddressEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		try {
			String id = request.getParameter("id");
			if(StringUtil.isNotEmpty(id)){
				result = companyExpressConfigAddressService.updateCompanyExpressConfigAddress(companyExpressConfigAddressEntity);
			}else{
				result = (int)companyExpressConfigAddressService.saveCompanyExpressConfigAddress(companyExpressConfigAddressEntity);
			}
			resultMap.put("code", result);
		} catch (Exception e) {
			handleException("添加公司常用物流配置-地址列表失败", logger, e);
		}
		return resultMap;
	}
	
}
